package com.menggudf.dao;

import com.menggudf.entity.MedicalRecord;
import com.menggudf.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

@Repository
public class MedicalRecordDao {
    
    private static final Log log = LogFactory.getLog(MedicalRecordDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(MedicalRecord transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(MedicalRecord persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public MedicalRecord merge(MedicalRecord detachedInstance) {
        try {
            MedicalRecord result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public MedicalRecord findById(int id) {
        try {
            MedicalRecord instance = entityManager.find(MedicalRecord.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<MedicalRecord> getMedicalRecords(
            String userCode,
            int pageSize,
            int offset) {
        try {
            StringBuffer sqld = new StringBuffer("from MedicalRecord m where m.usercode=?1 and m.status=1  order by m.createdTime desc");
            Query query = entityManager.createQuery(sqld.toString())
                    .setParameter(1, userCode).setFirstResult(offset).setMaxResults(pageSize);
            return query.getResultList();
        } catch (Exception e) {
            log.error("getMedicalRecords fail",e);
            throw e;
        }
    }

    public long getMedicalRecordsCount(String userCode) {
        try {
            StringBuffer sqld = new StringBuffer("select count(*) from MedicalRecord m where m.usercode=?1  and m.status=1 ");
            Query query = entityManager.createQuery(sqld.toString())
                    .setParameter(1, userCode);
            return (long) query.getSingleResult();
        } catch (Exception e) {
            log.error("getMedicalRecordsCount fail",e);
            throw e;
        }
    }

    public List<MedicalRecord> getMedicalRecordList(String userName, int pageSize, int offset) {
        try {
            StringBuffer sqld = new StringBuffer("from MedicalRecord m where 1=1 ");
            if(!StringUtils.isBlank(userName)){
                sqld.append(" and m.username=?1  order by m.createdTime desc ");
            }else{
                sqld.append(" order by m.createdTime desc ");
            }
            Query query = entityManager.createQuery(sqld.toString())
                    .setFirstResult(offset).setMaxResults(pageSize);
            if(!StringUtils.isBlank(userName)){
                query.setParameter(1, userName);
            }
            return query.getResultList();
        } catch (Exception e) {
            log.error("getMedicalRecordList fail",e);
            throw e;
        }
    }

    public long getMedicalRecordListCount(String userName) {
        try {
            StringBuffer sqld = new StringBuffer("select count(*) from MedicalRecord m   ");
            if(!StringUtils.isBlank(userName)){
                sqld.append(" where m.username=?1 ");
            }
            Query query = entityManager.createQuery(sqld.toString());
            if(!StringUtils.isBlank(userName)){
                query.setParameter(1, userName);
            }
            return (long) query.getSingleResult();
        } catch (Exception e) {
            log.error("getMedicalRecordListCount fail",e);
            throw e;
        }
    }

    public MedicalRecord findByCode(String code) {
        try {
            StringBuffer sqld = new StringBuffer("from MedicalRecord u where u.code=?1 ");
            Query query = entityManager.createQuery(sqld.toString()).setParameter(1, code);
            List<MedicalRecord> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("findByCode fail",e);
            throw e;
        }
    }
}
